package cn.hushi.test;

import cn.hushi.one2many.Customer;
import cn.hushi.one2many.Order;
import cn.hushi.utils.HibernateUtil;
import com.sun.org.apache.xpath.internal.operations.Or;
import org.hibernate.Session;
import org.hibernate.Transaction;
import org.junit.Test;

import java.util.Set;

public class Demo1 {
    @Test
    public void test1() {
//        客户表
        Customer customer = new Customer();
        customer.setId(1);
        customer.setName("韩立");
        customer.setGender("男");

        Customer customer2 = new Customer();
        customer2.setId(2);
        customer2.setName("灵瑶");
        customer2.setGender("女");
//        订单表
        Order order1 = new Order();
        order1.setOrderno("10010");
        order1.setProduct_name("九曲灵参");

        Order order2 = new Order();
        order2.setOrderno("10011");
        order2.setProduct_name("万年灵泉液");

        Order order3 = new Order();
        order3.setOrderno("10012");
        order3.setProduct_name("断魂草");

        Order order4 = new Order();
        order4.setOrderno("10013");
        order4.setProduct_name("八阶妖兽丹");

//        获取hibernate的Session
        Session session = HibernateUtil.getSession();
//        开启事务
        Transaction tx = session.beginTransaction();

//        建立一对多的关系(双向)
        customer.getOrders().add(order1);
        customer.getOrders().add(order2);
        customer2.getOrders().add(order3);
        customer2.getOrders().add(order4);
        order1.setCustomer(customer);
        order2.setCustomer(customer);
        order3.setCustomer(customer2);
        order4.setCustomer(customer2);


//        保存数据
        session.save(customer);
        session.save(customer2);
        session.save(order1);
        session.save(order2);
        session.save(order3);
        session.save(order4);

//        提交事务
        tx.commit();
//        关闭资源
        session.close();
    }

    /**
     * 查询操作
     */
    @Test
    public void test2() {
        Session session = HibernateUtil.getSession();
        Transaction tx = session.beginTransaction();

        Customer customer = session.get(Customer.class, 2);
        System.out.println(customer.getName());
        Set<Order> orders = customer.getOrders();
        for (Order order : orders) {
            System.out.println(order.getOrderno() + "," + order.getProduct_name());
        }

        System.out.println("-------------------------");
        Customer customer2 = session.get(Customer.class, 4);
        System.out.println(customer2.getName());
        Set<Order> orders2 = customer2.getOrders();
        for (Order order : orders2) {
            System.out.println(order.getOrderno() + "," + order.getProduct_name());
        }

        tx.commit();
        session.close();

    }

    /**
     * cascade级联配置
     */
    @Test
    public void test3() {
//              客户表
        Customer customer = new Customer();
        customer.setId(1);
        customer.setName("韩立");
        customer.setGender("男");

        Customer customer2 = new Customer();
        customer2.setId(2);
        customer2.setName("灵瑶");
        customer2.setGender("女");
//        订单表
        Order order1 = new Order();
        order1.setOrderno("10010");
        order1.setProduct_name("九曲灵参");

        Order order2 = new Order();
        order2.setOrderno("10011");
        order2.setProduct_name("万年灵泉液");

        Order order3 = new Order();
        order3.setOrderno("10012");
        order3.setProduct_name("断魂草");

        Order order4 = new Order();
        order4.setOrderno("10013");
        order4.setProduct_name("八阶妖兽丹");

//        获取hibernate的Session
        Session session = HibernateUtil.getSession();
//        开启事务
        Transaction tx = session.beginTransaction();

//        建立一对多的关系
        customer.getOrders().add(order1);
        customer.getOrders().add(order2);
        customer2.getOrders().add(order3);
        customer2.getOrders().add(order4);
        order1.setCustomer(customer);
        order2.setCustomer(customer);
        order3.setCustomer(customer2);
        order4.setCustomer(customer2);


//        保存数据
        session.save(customer);
        session.save(customer2);
        session.save(order1);
        session.save(order2);
        session.save(order3);
        session.save(order4);

//        提交事务
        tx.commit();
//        关闭资源
        session.close();
    }
}